<template>
  <a-drawer
    :title="title"
    :width="width"
    placement="right"
    :closable="false"
    @close="close"
    :visible="visible">
  
    <a-spin :spinning="confirmLoading">
      <a-form :form="form">
      <a-row>

	<a-col :span="12">
        <a-form-item label="仓库编号" :labelCol="labelCol" :wrapperCol="wrapperCol">
          <a-input v-decorator="['warehouseId', validatorRules.warehouseId]" placeholder="请输入仓库编号"></a-input>
        </a-form-item>
        </a-col>
	<a-col :span="12">
        <a-form-item label="仓库编码" :labelCol="labelCol" :wrapperCol="wrapperCol">
          <a-input v-decorator="['warehouseCode', validatorRules.warehouseCode]" placeholder="请输入仓库编码"></a-input>
        </a-form-item>
        </a-col>
	<a-col :span="12">
        <a-form-item label="仓库名称" :labelCol="labelCol" :wrapperCol="wrapperCol">
          <a-input v-decorator="['warehouseName', validatorRules.warehouseName]" placeholder="请输入仓库名称"></a-input>
        </a-form-item>
        </a-col>
	<a-col :span="12">
        <a-form-item label="建设冷冻容量" :labelCol="labelCol" :wrapperCol="wrapperCol">
          <a-input-number v-decorator="['buildingFreeze']" placeholder="请输入建设冷冻容量" style="width: 100%"/>
        </a-form-item>
        </a-col>
	<a-col :span="12">
        <a-form-item label="建设冷藏容量" :labelCol="labelCol" :wrapperCol="wrapperCol">
          <a-input-number v-decorator="['buildingRefrigerate']" placeholder="请输入建设冷藏容量" style="width: 100%"/>
        </a-form-item>
        </a-col>
	<a-col :span="12">
        <a-form-item label="建设恒温容量" :labelCol="labelCol" :wrapperCol="wrapperCol">
          <a-input-number v-decorator="['buildingConstant']" placeholder="请输入建设恒温容量" style="width: 100%"/>
        </a-form-item>
        </a-col>
	<a-col :span="12">
        <a-form-item label="建设常温容量" :labelCol="labelCol" :wrapperCol="wrapperCol">
          <a-input-number v-decorator="['buildingNormal']" placeholder="请输入建设常温容量" style="width: 100%"/>
        </a-form-item>
        </a-col>
	<a-col :span="12">
        <a-form-item label="已使用冷冻" :labelCol="labelCol" :wrapperCol="wrapperCol">
          <a-input-number v-decorator="['physicalFreeze']" placeholder="请输入已使用冷冻" style="width: 100%"/>
        </a-form-item>
        </a-col>
	<a-col :span="12">
        <a-form-item label="已使用冷藏" :labelCol="labelCol" :wrapperCol="wrapperCol">
          <a-input-number v-decorator="['physicalRefrigerate']" placeholder="请输入已使用冷藏" style="width: 100%"/>
        </a-form-item>
        </a-col>
	<a-col :span="12">
        <a-form-item label="已使用恒温" :labelCol="labelCol" :wrapperCol="wrapperCol">
          <a-input v-decorator="['physicalConstant']" placeholder="请输入已使用恒温"></a-input>
        </a-form-item>
        </a-col>
	<a-col :span="12">
        <a-form-item label="已使用常温" :labelCol="labelCol" :wrapperCol="wrapperCol">
          <a-input v-decorator="['physicalNormal']" placeholder="请输入已使用常温"></a-input>
        </a-form-item>
        </a-col>
	<a-col :span="12">
        <a-form-item label="备注" :labelCol="labelCol" :wrapperCol="wrapperCol">
          <a-input v-decorator="['remark']" placeholder="请输入备注"></a-input>
        </a-form-item>
        </a-col>
        </a-row>
      </a-form>
    </a-spin>
    <a-button type="primary" @click="handleOk">确定</a-button>
    <a-button type="primary" @click="handleCancel">取消</a-button>
  </a-drawer>
</template>

<script>

  import { httpAction } from '@/api/manage'
  import pick from 'lodash.pick'
  import { validateDuplicateValue } from '@/utils/util'
  export default {
    name: "CrmWmsInventoryModal",
    components: { 
    },
    data () {
      return {
        form: this.$form.createForm(this),
        title:"操作",
        width:800,
        visible: false,
        model: {},
        labelCol: {
          xs: { span: 24 },
          sm: { span: 5 },
        },
        wrapperCol: {
          xs: { span: 24 },
          sm: { span: 16 },
        },
        confirmLoading: false,
        validatorRules: {
          warehouseId: {
            rules: [
              { required: true, message: '请输入仓库编号!'},
            ]
          },
          warehouseCode: {
            rules: [
              { required: true, message: '请输入仓库编码!'},
            ]
          },
          warehouseName: {
            rules: [
              { required: true, message: '请输入仓库名称!'},
            ]
          },
        },
        url: {
          add: "/online/api/createCrmWmsInventory",
          edit: "/online/api/updateCrmWmsInventory",
          get: "/online/api/getCrmWmsInventory",
        }
      }
    },
    created () {
    },
    methods: {
      add () {
        this.edit({});
      },
      edit (record) {
        this.form.resetFields();
        if(record.id){
	      httpAction(this.url.get,{id:record.id},"post").then((res)=>{
	         if(res.success){
		        this.model = Object.assign({}, res.result);
		        this.visible = true;
		        this.$nextTick(() => {
		           this.form.setFieldsValue(pick(this.model,'warehouseId','warehouseCode','warehouseName','buildingFreeze','buildingRefrigerate','buildingConstant','buildingNormal','physicalFreeze','physicalRefrigerate','physicalConstant','physicalNormal','remark','createUserId','ownerUserId','ownerBuId','createTime','updateTime'))
		        })   
	         }
	      })
        }
      },
      close () {
        this.$emit('close');
        this.visible = false;
      },
      handleOk () {
        const that = this;
        // 触发表单验证
        this.form.validateFields((err, values) => {
          if (!err) {
            that.confirmLoading = true;
            let httpurl = '';
            let method = '';
            if(!this.model.id){
              httpurl+=this.url.add;
              method = 'post';
            }else{
              httpurl+=this.url.edit;
               method = 'put';
            }
            let formData = Object.assign(this.model, values);
            console.log("表单提交数据",formData)
            httpAction(httpurl,formData,method).then((res)=>{
              if(res.success){
                that.$message.success(res.message);
                that.$emit('ok');
              }else{
                that.$message.warning(res.message);
              }
            }).finally(() => {
              that.confirmLoading = false;
              that.close();
            })
          }
         
        })
      },
      handleCancel () {
        this.close()
      },
      popupCallback(row){
        this.form.setFieldsValue(pick(row,'warehouseId','warehouseCode','warehouseName','buildingFreeze','buildingRefrigerate','buildingConstant','buildingNormal','physicalFreeze','physicalRefrigerate','physicalConstant','physicalNormal','remark','createUserId','ownerUserId','ownerBuId','createTime','updateTime'))
      }
      
    }
  }
</script>

<style lang="less" scoped>
/** Button按钮间距 */
  .ant-btn {
    margin-left: 30px;
    margin-bottom: 30px;
    float: right;
  }
</style>
